﻿using System;
using System.IO;
using Core.Authenticated;
using IZ.WebFileManager;
using IZ.WebFileManager.Components;

namespace CMS.Controls
{
    public partial class IZFileManager : AuthenticatedPage
    {
        public string Type = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            Type = Request.QueryString["type"];

            var folder = "~/Data/UserData/" + CurrentUser.AdminUserId;
            var folderPath = Server.MapPath(folder);
            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            FileManager1.FileViewMode = FileViewRenderMode.Thumbnails;
            FileManager1.RootDirectories.Clear();
            var rootDirectory = new RootDirectory { DirectoryPath = folder };
            FileManager1.RootDirectories.Add(rootDirectory);
            FileManager1.RootDirectories[0].ShowRootIndex = false;
        }

        protected void LogSelected(object sender, EventArgs e)
        {
            lbMessage.Text = string.Empty;

            if (FileManager1.SelectedItems.Length == 0)
                lbMessage.Text = @"Bạn chưa chọn file nào.<br />";

            switch (Type)
            {
                case "article":
                    if (FileManager1.SelectedItems.Length > 1)
                        lbMessage.Text = @"Bạn chỉ được chọn 1 file.<br />";
                    else
                    {
                        var newsFile = FileManager1.SelectedItems[0].VirtualPath;

                        if (newsFile.IndexOf(".jpg") != -1 || newsFile.IndexOf(".png") != -1 || newsFile.IndexOf(".gif") != -1 || newsFile.IndexOf(".bmp") != -1)
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "citinews", "<script>returnValue('" + FileManager1.SelectedItems[0].VirtualPath + "');</script>");
                        else
                            return;
                    }                    
                    break;
               
                case "data":
                    if (FileManager1.SelectedItems.Length > 1)
                        lbMessage.Text = @"Bạn chỉ được chọn 1 file.<br />";
                    else
                    {
                        var newsFile = FileManager1.SelectedItems[0].VirtualPath;

                        if (newsFile.ToLower().IndexOf(".xls") != -1 || newsFile.ToLower().IndexOf(".xlsx") != -1)
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "citinews", "<script>returnValue('" + FileManager1.SelectedItems[0].VirtualPath + "');</script>");
                        else
                            return;
                    }                    
                    break;

                case "multiple":
                    foreach (FileManagerItemInfo item in FileManager1.SelectedItems)
                    {
                        if (item.VirtualPath.ToLower().IndexOf(".jpg") != -1 || item.VirtualPath.ToLower().IndexOf(".png") != -1 || item.VirtualPath.ToLower().IndexOf(".gif") != -1 || item.VirtualPath.ToLower().IndexOf(".bmp") != -1)
                            SelectedItemsLog.Text += item.VirtualPath + "►";
                        else
                        {
                            lbMessage.Text = @"Bạn vui lòng chọn file có đuôi *.jpg, *.png, *.gif, *.bmp";
                            return;
                        }
                    }

                    ClientScript.RegisterClientScriptBlock(this.GetType(), "quiz", "<script>returnValue('" + SelectedItemsLog.Text + "');</script>");
                    break;
                case "slideshow":
                    foreach (FileManagerItemInfo item in FileManager1.SelectedItems)
                    {
                        if (item.VirtualPath.ToLower().IndexOf(".jpg") != -1 || item.VirtualPath.ToLower().IndexOf(".png") != -1 || item.VirtualPath.ToLower().IndexOf(".gif") != -1 || item.VirtualPath.ToLower().IndexOf(".bmp") != -1)
                            SelectedItemsLog.Text += item.VirtualPath + "►";
                        else
                        {
                            lbMessage.Text = @"Bạn vui lòng chọn file có đuôi *.jpg, *.png, *.gif, *.bmp";
                            return;
                        }
                    }

                    ClientScript.RegisterClientScriptBlock(this.GetType(), "quiz", "<script>returnValue('" + SelectedItemsLog.Text + "');</script>");
                    break;
            }
        }
    }
}